<div class="loading-container"><div class="loader"></div></div>

<div class="wrapper wrapper-shadow wrapper-list"> 
	<!-- list data wrapper -->
 	<div class="result-wrapper no-padding">
   	<div class="result-body">
   		<div class="tabs">
      	<ul class="nav nav-tabs widget no-margin-bottom">
        	<li class="active"> 
	         	<a href="#tab-content" data-toggle="tab" onclick="handlerStatus(this, '0')" data-table-toggle="handlerButtons('newadd');" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 
	         		<span class="menu-icon"><i class="fa fa-user"></i></span> 非会员
	         	</a>
          </li>
          <li>
	         	<a href="#tab-content" data-toggle="tab" onclick="handlerStatus(this, 'member')" data-table-toggle="handlerButtons('member');" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true"> 
	         		<span class="menu-icon"><i class="fa fa-users"></i></span> 会员 
	         	</a>
          </li>
				</ul>
        <div class="tab-content no-border xs-padding">
        	<div class="tab-pane active" id="tab-content">
           	<div id="toolbar">
         		 	<button type="button" class="btn btn-primary" onclick="handlerCreate(this);" data-editor-path="/miwawa/content/esm/customer/editor/" data-editor-page="add.html" id="newadd">
			        	<i class="glyphicon glyphicon-plus"></i> 新增
					    </button>
					    
					    <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="btn_adviser" style="display: none;">
						    <i class="glyphicon glyphicon-random"></i> 分配
						  </button>
						  
							<ul class="dropdown-menu list-adviser"></ul>
							
           		<div class="btn-group btn-group-filter btn-group-newadd">
						    <button type="button" class="btn btn-default active" onclick="handlerStatus(this, '0')"       data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">新增的</button>
						    <button type="button" class="btn btn-default" 			 onclick="handlerStatus(this, 'follows')" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">跟进的</button>
						    <button type="button" class="btn btn-default" 			 onclick="handlerStatus(this, 'promise')" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">诺访的</button>
						    <button type="button" class="btn btn-default" 			 onclick="handlerStatus(this, 'comming')" data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">到店的</button>
						    <button type="button" class="btn btn-default" 			 onclick="handlerStatus(this, 'losted')"  data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">流失的</button>
           		</div>
           		
           		<div class="btn-group btn-group-filter btn-group-member" style="display: none;">
						    <button type="button" class="btn btn-default active" onclick="handlerStatus(this, 'member')"  data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">有效的</button>
						    <button type="button" class="btn btn-default" 			 onclick="handlerStatus(this, 'expire')"  data-table-target="#table-customer" data-form-search="#search-form-customer" data-columns="true">过期的</button>
           		</div>
						</div>
           	<table data-ajax="handlerSearch" 
           				 data-method="post" 
           				 data-search="true" 
           				 data-toolbar="#toolbar" 
           				 data-id-field="id" 
           				 data-unique-id="id" 
           				 data-pagination="true" 
           				 data-show-toggle="true" 
           				 data-show-refresh="true" 
           				 data-show-columns="true" 
           				 data-check-on-init="true" 
           				 data-click-to-select="true" 
           				 data-side-pagination="server" 
           				 data-mobile-responsive="true" 
           				 data-event-onload="handlerSuccess" 
           				 data-form-search="#search-form-customer" id="table-customer" class="bootstrap-table">
							<thead>
								<tr>
									<th data-checkbox="true"></th>
									<th data-field="id"						data-visible="false" data-switchable="false">ID</th>
									<th data-field="createDate" 	data-align="center" data-sortable="true">登记时间</th>
									<th data-field="username" 		data-align="center" data-sortable="true">顾问</th>
									<th data-field="student" 			data-align="center" >宝宝</th>
									<th data-field="moonage" 			data-align="center" data-sortable="true">月龄</th>
									<th data-field="remark" 			data-halign="center" >宝宝备注</th>
									<th data-field="relation" 		data-align="center" >关系</th>
									<th data-field="parent" 			data-align="center" data-sortable="true">家长姓名</th>
									<th data-field="mobile" 			data-align="center" data-sortable="true">联系方式</th>
									<th data-field="app" 					data-align="center" data-sortable="true">APP昵称</th>
									<th data-field="market" 			data-align="center" data-sortable="true">渠道</th>
									<th data-field="source" 			data-align="center" data-sortable="true">来源</th>
									<th data-field="operator" 		data-align="center" data-width="130" data-formatter="renderOperator" data-table-target="customer">操作</th>
								</tr>
							</thead>
						</table>
        	</div>
      	</div>
    	</div>
   	</div>
 	</div>

	<!-- form editor wrapper -->
	<div class="editor-wrapper">
		
	</div>
</div>
<!-- PAGE CONTENT END =============================================== -->

<div class="hidden">
	<table data-ajax="handlerSearch" 
    		 data-method="post" 
			 	 data-id-field="id" 
			 	 data-unique-id="id" 
				 data-pagination="true" 
				 data-check-on-init="true" 
				 data-click-to-select="true" 
				 data-side-pagination="server" 
				 data-mobile-responsive="true" 
				 data-event-onload="handlerLoaded" 
				 data-form-search="#search-form-adviser" id="table-adviser" class="bootstrap-table">
		<thead>
			<tr>
				<th data-radio="true"></th>
				<th data-field="id" data-visible="false">ID</th>
				<th data-field="name" 		data-align="center" >姓名</th>
				<th data-field="mobile" 	data-align="center" >手机</th>
				<th data-field="newadds" 	data-align="center" >当月新增客户</th>
				<th data-field="follows" 	data-align="center" >当月跟进客户</th>
				<th data-field="members" 	data-align="center" >当月签约客户</th>
			</tr>
		</thead>
	</table>
</div>

<!-- 查询表单 ========================================================= -->
<form id="search-form-customer" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.view.CustomerView">
			<input type="hidden" name="userid" value="" 				data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" name="isparent" value="true" 	data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" name="status" value="0" 				data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" name="invalid" value="F" 			data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" data-search-sort="createDate" 	data-search-order="desc"/>
			<input type="hidden" data-search-sort="id" 					data-search-order="asc"/>
</form>
<form id="search-form-adviser" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.view.AdviserView">
			<input type="hidden" name="status" value="0" 				data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" name="invalid" value="F" 			data-search-group="and" data-search-operation="EQ"/>
			<input type="hidden" data-search-sort="createDate" 	data-search-order="desc"/>
			<input type="hidden" data-search-sort="id" 					data-search-order="asc"/>
</form>
<!-- 删除表单 ========================================================= -->
<form id="reback-form-customer" action="/miwawa/{dbname}/customer/reback" method="post" name="com.app.miwawa.api.esm.entity.customer.Customer"></form>
<form id="remove-form-customer" action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Customer"></form>
<form id="remove-form-selling" 	action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Selling"></form>
<form id="remove-form-bespoke" 	action="/miwawa/{dbname}/common/remove" 	method="post" name="com.app.miwawa.api.esm.entity.customer.Bespoke"></form>

<!-- Javascript ===================================================== -->
<script src="/miwawa/resources/lib/miwawa/1.0.2/js/core/requirejs/require.js"></script>

<script th:inline="javascript">
/*<![CDATA[*/
<!--
	var auths = /*[[${data.result.auths}]]*/;
	var authstr = "";
	var authsmap = {"miwawa.esm.customer.adviser":"#btn_adviser"};
	var $moment;
	var promisedate;
	
	/* ===================================== */
	/* 列表页面 */
	/* ===================================== */
	
	// 注册列表核心组件
	jQuery(function(){
		jQuery(":hidden[name='userid']","#search-form-customer").val(userid);
		
		var array = [];
		for(var i in auths){
			jQuery(authsmap[auths[i].code]).show();
			if(auths[i].code == "miwawa.esm.customer.adviser"){
				jQuery(":hidden[name='userid']","#search-form-customer").val("");
			}
		}
		
		require(['/miwawa/resources/lib/miwawa/1.0.2/js/core/module/app/simple.js','moment'], function(simple,moment){
			$moment = moment;
			simple(['registerForm','registerTable','registerWidget']);
		});
	});
	
	function handlerButtons(tags){
		switch(tags){
			case "newadd":
				jQuery('button#newadd').show();
				jQuery('.btn-group-newadd').show();
				jQuery('.btn-group-member').hide();
			break;
			case "member":
				jQuery('button#newadd').hide();
				jQuery('.btn-group-newadd').hide();
				jQuery('.btn-group-member').show();
			break;
		}
	}
	
	// TODO 顾问列表加载完后调用
	function handlerLoaded(data){
		// 创建顾问下拉选单
		jQuery(".list-adviser").append(function(){
			var html = "";
			jQuery.each(data.rows,function(i,adviser){
				html+="<li><a href='javascript:void(0);' class='selected-dropdown' data-selected-id='"+adviser.id+"'>"+adviser.name+"</a></li>"
			});
			return html;
		});
		
		// 绑定顾问下拉选单的选中时间
		jQuery('.list-adviser').delegate('a.selected-dropdown', 'click', function() {
			var rows = jQuery("#table-customer").bootstrapTable("getSelections");
			if(rows.length <= 0){
				jQuery.message.warning("请选择要分配顾问的客户！");
			}else{
				handlerSelected(this);
			}
		});
		
		// 分配销售顾问
		function handlerSelected(_this){
			var ids = [];
			var url = "/miwawa/{dbname}/customer/update/{ids}".replace("{dbname}", dbname);
			var rows = jQuery("#table-customer").bootstrapTable("getSelections");
			
			jQuery.each(rows, function(index, row){
				ids.push(row.customerid);			
			});
			
			jQuery.ajaxsend({
				url:url.replace("{ids}", ids.join(",")),
				data:{
					"userid":jQuery(_this).data("selected-id"),
					"username":jQuery(_this).text()
				},
				success:function(data){
					if(data && data["success"]) {
						jQuery("#table-customer").bootstrapTable('refresh');
					}
				}
			});
		}
	};
	
	// 渲染列表意向程度列
	var intention = ["","差","较差","一般","强","非常强"]
	function renderIntention(value, row ){
		return row.id != "new" ? value ? intention[value] : "未跟进" : value;
	}
	
	// 渲染列表操作列
	function renderOperator(value, row, index){
		var operator = "";
		switch(this.tableTarget){
			case "customer":
				if(row.status == "losted"){
					operator +="<a class='btn btn-info 	  btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/esm/customer/editor' data-editor-page='view.html' data-toggle='tooltip' data-placement='top' title='查看'> <i class='icon-call-out'></i> </a>" 
										+"<a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='还原' onclick='handlerReback(this, \""+row.customerid+"\")' data-table-source='#table-customer' data-form-source='#reback-form-customer' data-custom='true'> <i class='fa fa-reply'></i> </a>"
									  +"<a class='btn btn-danger  btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.customerid+"\");deleteEasemob(\""+row.customerid+"\")' data-table-source='#table-customer' data-form-source='#remove-form-customer'> <i class='fa fa-trash'></i> </a>";
				}else{
					operator += "<a class='btn btn-info 	btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/esm/customer/editor' data-editor-page='edit.html' data-toggle='tooltip' data-placement='top' title='查看与跟进'> <i class='icon-call-out'></i> </a>"
				 	 				 + "<a class='btn btn-success btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/esm/customer/editor' data-editor-page='sign.html' data-toggle='tooltip' data-placement='top' title='报名与签约'> <i class='icon-loop'></i> </a>";
				}
				
				if(row.status == "comming"){
					operator+= "<a class='btn btn-warning  btn-xs btn-table-op' onclick='followAgain(\""+row.bespokeDate+"\",\""+row.promiseDate+"\",\""+row.customerid+"\")' data-toggle='tooltip' data-placement='top' title='重新跟进'> <i class='fa fa-undo'></i> </a>";
				}
				
				if(row.status != "losted" && row.status != "member"){
					operator+= "<a class='btn btn-danger  btn-xs btn-table-op' onclick='handlerCreate(this,\""+row.id+"\")' data-table-source='#table-customer' data-editor-path='/miwawa/content/esm/customer/editor' data-editor-page='lost.html' data-toggle='tooltip' data-placement='left' title='客户流失'> <i class='icon-user-unfollow'></i> </a>";
				}
			break;
			case "selling":
				if(row.id != "new"){
					operator = "<a class='btn btn-danger btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.id+"\")' data-table-source='#table-selling' data-form-source='#remove-form-selling'> <i class='fa fa-trash'></i> </a>";
				}else{
					operator = value;
				}
			break;
			case "bespoke":
				if(row.id == "new" || row.id == "edit"){
					operator = value;
				}else{
					operator = "<a class='btn btn-info	btn-xs btn-table-op' id='edited' onclick='editBespoke(this,\""+index+"\")' data-table-source='#table-bespoke' data-rowdata='"+JSON.stringify(row)+"' data-toggle='tooltip' data-placement='top' title='修改'> <i class='ion-compose'></i> </a>"
					 + "<a class='btn btn-danger btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='删除' onclick='handlerRemove(this, \""+row.id+"\")' data-table-source='#table-bespoke' data-form-source='#remove-form-bespoke'> <i class='fa fa-trash'></i> </a>";
				}
			break;
		}
		return operator;
	}
	
	// 列表字段定义
	var columns = {
		"0":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'createDate',	title: '登记时间' },
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'remark', 			title: '宝宝备注' },
			{ field: 'parent', 			title: '家长姓名' },
			{ field: 'relation', 		title: '关系' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'app', 				title: 'APP昵称', formatter:function(value){return value} },
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'operator', 		title: '操作' }		
		],
		"follows":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'createDate',	title: '时间' },
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'follows', 		title: '跟进次数' },
			{ field: 'lastDate', 		title: '最近跟进', formatter:tooltipContent},
			{ field: 'nextDate', 		title: '下次跟进', formatter:function(value){return value} },
			{ field: 'intention', 	title: '意向', formatter:function(value){return intention[value]} },
			{ field: 'comming', 		title: '到店次数', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		],
		"promise":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'createDate',	title: '时间' },
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'remark', 			title: '宝宝备注' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'bespoke', 		title: '诺坊次数' },
			{ field: 'promiseDate', title: '最近诺坊', formatter:function(value){return value} },
			{ field: 'bespokeType', title: '诺坊类型', formatter:function(value){return value} },
			{ field: 'comming', 		title: '到店次数', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		],
		"comming":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'createDate',	title: '时间' },
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'remark', 			title: '宝宝备注' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'follows', 		title: '跟进次数' },
			{ field: 'promiseDate', title: '最近诺坊', formatter:function(value){return value} },
			{ field: 'commingDate', title: '最近到店' },
			{ field: 'comming', 		title: '到店次数', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		],
		"losted":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'createDate',	title: '操作时间' },
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'remark', 			title: '宝宝备注' },
			{ field: 'relation', 		title: '家长' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'lostedDate', 	title: '流失时间' },
			{ field: 'lostedReason',title: '流失原因',},
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'operator', 		title: '操作' }	
		],
		"member":[
			{ checkbox: true }, 
			{ field: 'id', 					title: 'ID',	visible: false, switchable:false }, 
			{ field: 'username', 		title: '顾问' },
			{ field: 'student', 		title: '宝宝' },
			{ field: 'moonage', 		title: '月龄' },
			{ field: 'remark', 			title: '宝宝备注' },
			{ field: 'relation', 		title: '关系' },
			{ field: 'parent', 			title: '家长姓名' },
			{ field: 'mobile', 			title: '联系方式' },
			{ field: 'market', 			title: '渠道' },
			{ field: 'source', 			title: '来源' },
			{ field: 'lastDate', 		title: '最近跟进', formatter:tooltipContent},
			{ field: 'nextDate', 		title: '下次跟进', formatter:function(value){return value} },
			{ field: 'operator', 		title: '操作' }		
		]
	};
	
	function tooltipContent(value, row, index){
		return "<a style='cursor:pointer' data-toggle='tooltip' data-placement='top' title='"+row.content+"'>"+value+"</a>";
	}	
	/* ===================================== */
	/* 新增及编辑页面 */
	/* ===================================== */
	
	// 新增家长输入表单后的回调
	function handlerParents(options,data){
		if(options.id){
			var last = jQuery(".form-editor:last", ".editor-body-parents");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Parents");
		}else{
			require(['jquery.plugin.form'],function(form){
				var relation = jQuery("#relation",".form-editor-parents-"+options.index);
				if(relation.length > 0){
					form.chosen(relation).select(relation);
					form.tooltip("[data-toggle='tooltip']");
				}
			});
		} 
	}
	
	// 新增宝宝输入表单后的回调
	function handlerStudent(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-student");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
			
			// 更新头像信息
			var avatar = last.find(":hidden[name='avatar']").next("a");
			avatar.data("update",true);
			avatar.data("pk",options.id);
			avatar.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
		}else{
			require(['jquery.plugin.form'],function(form){
				form.datepicker(jQuery(".date-selector",".form-editor-student-"+options.index));
				form.tooltip("[data-toggle='tooltip']");
			});
		}
	}
	
	// 新增地址输入表单后的回调
	function handlerAddress(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-address");
			last.find(":hidden[name='id']").val(options.id);
		}
	}
	
	// 打开模态窗口
	function handlerModal(trigger, url, before, callback){
		jQuery("#extension-modal").removeClass("modal-middle").data("url",url).data("onshow",function(){
			if(before) window[before](trigger);
		}).data("onhidden",function(){
			if(callback) window[callback](trigger);
		}).modal("show");
	}
	
	// 试听排班模态窗口
	function handlerAudition(trigger, rowid, promise){
		promisedate = promise;	
		jQuery("#extension-modal").removeClass("modal-middle").data("selectedRow", jQuery("#table-bespoke").bootstrapTable("getRowByUniqueId",rowid))
															.data("url","content/esm/customer/dialog/classes.html").data("onshow",function(){
																var $table = jQuery("#table-classes");
																if($table.hasClass("table")){
																	$table.bootstrapTable("refresh");
																}else{
																	$table.bootstrapTable();
																}
															}).modal("show");
	}
	
	// 到店
	function handlerComming(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid,
									"commingDate":moment().format("YYYY-MM-DD HH:mm"),
									"commingState":"0"
					       }
				},
				success:function(data){
					jQuery.ajaxsend({
						url:"/miwawa/{dbname}/customer/audition/edit/{bespokeid}".replace("{dbname}",dbname).replace("{bespokeid}",rowid),
						success:function(){
							jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
							jQuery("#table-bespoke").bootstrapTable("refresh");
						}
					});
				}
			});
		});
	}
	
	// 未到店
	function notTo(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid, "commingState":"1"}
				},
				success:function(data){
					jQuery.ajaxsend({
						url:"/miwawa/{dbname}/customer/audition/edit/{bespokeid}".replace("{dbname}",dbname).replace("{bespokeid}",rowid),
						success:function(){
							jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
							jQuery("#table-bespoke").bootstrapTable("refresh");
						}
					});
				}
			});
		});
	}
	
	// 渲染列表试听列
	function renderAudition(value, row, index){
		if(value == "试听" && row.classes){
			return value+" "+row.classes;
		}else if(value == "试听" && row.id != "new" && row.commingState == ""){
			return value+" <a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='试听' onclick='handlerAudition(this, \""+row.id+"\", \""+row.promiseDate+"\")'> <i class='icon-earphones'></i> </a>";			
		}else if(value == "试听" && row.commingState != ""){
			return value;
		}else{
			return value;			
		}
	}
	
	// 渲染列表到访列
	function renderComming(value, row, index){
		if(row.id == "edit"){
			return ""
		}else{
			if(row.commingState == "" ){
				return "<a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='到店' onclick='handlerComming(this, \""+row.id+"\")'> <i class='fa fa-check'></i> </a>&nbsp;到店"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn-warning btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='未到店' onclick='notTo(this, \""+row.id+"\")'> <i class='fa fa-times'></i> </a>&nbsp;未到店";			
			}else if(row.commingState == "1"){
				return "未到店";			
			}else if(row.commingState == "0"||row.commingState == "2"){
				return "已到店";			
			}else{
				return "";
			}
		}
	}
	
	/* ===================================== */
	/* 表格 行编辑 开始 */
	/* ===================================== */
	// 添加行编辑
	function handlerAddRow(target){
		var $table = jQuery(target);
		
		if(!hasEditorRow()){
			var rowdata = {
				"table-selling":{
					id:'new',
	        createDate: createInput('createDate', 'text', 		'ext-time'),
	        intention: 	createInput('intention', 	'text', 		'ext-rate', '3'),
	        content: 		createInput('content', 		'text', 		'ext-text'),
	        nextDate: 	createInput('nextDate', 	'text', 		'ext-date', $moment().add(3, 'd').format("YYYY-MM-DD HH:mm")),
	        username:		createInput('username', 	'readonly', 'ext-text', username),
	        operator: 	createButton(target)
				},
				"table-bespoke":{
					id:'new',
          createDate: 	createInput('createDate', 	'text', 		'ext-time'),
          bespokeType: 	createInput('bespokeType', 	'select', 	'ext-text'),
          promiseDate: 	createInput('promiseDate',	'text', 		'ext-date'),
          username:			createInput('username', 		'readonly', 'ext-text', username),
          operator: 		createButton(target)
				}
			};
			
			$table.bootstrapTable('insertRow', { index: 0, row: rowdata[target.replace("#","")] });
			
			setTimeout(function(){
				require(['jquery.plugin.form'],function(form){
					form.rating(".ext-rate");
					form.datepicker(".ext-time",{format: "YYYY-MM-DD HH:mm"});
					form.datepicker(".ext-date",{format: "YYYY-MM-DD HH:mm"});
				});
			},0);
		}
		
		// 创建输入框
		function createInput(name, type, ext, value){
			switch(type){
				case 'text': 
					var input = "";
					if(value != undefined){
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'" value="'+value+'">';
					}else{
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'">';
					}
					return input;
				case 'readonly': 
					input = '<input name="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+value+'" readonly="readonly">';
					if(name=="username"){
						input+='<input name="userid" type="hidden" value="'+userid+'">';
					}
					return input;
				case 'select': 
					var select='<select name="'+name+'" id="'+name+'" class="form-control" style="width:80px;">'
									  +	'<option value="看店">看店</option>'
									  +	'<option value="试听">试听</option>'
									  +	'<option value="签约">签约</option>'
									  +	'<option value="付款">付款</option>'
									  +'</select>';
				return select;
			}
		}
		
		// 创建按钮
		function createButton(target){
			return ""
						+"<span class='table-editor-tools'>"
						+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\");'><i class='icon ion-close-circled'></i></button>"
						+"	<button type='button' class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\",\"validated\");'><i class='icon ion-checkmark-circled'></i></button>"
						+"</span>";
		}
		
		// 判断是否有编辑行
		function hasEditorRow(){
			return $table.bootstrapTable('getRowByUniqueId', 'new') ? true : false;
			}
	}
	
	//修改诺访记录
	var bespoke;
	function editBespoke(trigger,index){
		jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip("destroy");
		
		var _opts = jQuery(trigger).data();
		bespoke = _opts.rowdata;
		
		jQuery("#table-bespoke").bootstrapTable("updateRow",{
			index: index,
			row: editRows(_opts.rowdata, _opts.tableSource)
		});
		
		//修改诺访
		function editRows(row,target){
			return {
				id:   				"edit",
				createDate: 	row.createDate,
				bespokeType: 	foundInput('bespokeType'),
				promiseDate: 	row.promiseDate,
				username: 		row.username,
				userid: 			row.userid,
	      operator: 		foundButton()
			};
		}
		
		// 创建诺访类型选择框
		function foundInput(name){
	    var select = '<select name="'+name+'" id="'+name+'" class="form-control" style="width:80px;">'
						     + '<option value="看店">看店</option>'
								 + '<option value="试听">试听</option>'
								 + '<option value="签约">签约</option>'
								 + '<option value="付款">付款</option>'
								 + '</select>';
			return select;
		}
		
		// 创建更新诺访记录后的提交及取消按钮
		function foundButton(){
			return ""
				+"<span class='table-editor-tools'>"
				+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handCanced()' title='取消'><i class='icon ion-close-circled'></i></button>"
				+"	<button type='button' class='btn btn-success btn-xs' onclick='handSured()' title='确认'><i class='icon ion-checkmark-circled'></i></button>"
				+"</span>";
		}
		
	}
	
	//更新诺访记录
 	function handSured(){
	 	jQuery.ajaxsend({
	 		url:"/miwawa/{dbname}/common/update".replace("{dbname}", dbname),
	 		data:{
	 				"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":bespoke.id,"bespokeType":jQuery("#bespokeType").val()}
	 		},
	 		success:function(data){
	 			jQuery.ajaxsend({
					url:"/miwawa/{dbname}/customer/bespoke/edit/{bespokeid}".replace("{dbname}", dbname).replace("{bespokeid}",bespoke.id),
					success:function(){
						jQuery("#table-bespoke").bootstrapTable("refresh");
					}
				});
	 		}
		});
	}
 	
	//取消更新诺访记录
	function handCanced(){
		jQuery("#table-bespoke").bootstrapTable("refresh");
	}
	
	
	//跟进输入框必填
	function validated(){
		if(jQuery("#content").val()!=undefined){
			if(jQuery("#createDate",".table-editor-selling").val()==""){
				jQuery.message.alert("请输入通话时间！");
				return false;
			}else if(jQuery("#content").val()==""){
				jQuery.message.alert("请输入跟踪情况！");
				return false;
			}else if(jQuery("#content").val().length > 100){
				jQuery.message.alert("跟踪情况字数限制在100以内！");
				return false;
			}else if(jQuery("#nextDate").val()==""){
				jQuery.message.alert("请输入下次沟通时间！");
				return false;
			}else{
				return true;			
			}
		}else{
			if(jQuery("#createDate",".table-editor-bespoke").val()==""){
				jQuery.message.alert("请输入预约时间！");
				return false;
			}else if(jQuery("#promiseDate").val()==""){
				jQuery.message.alert("请输入诺访时间！");
				return false;
			}else{
				var selling = [];
				var customer = jQuery("#table-customer").data("selectedRow");
				selling.push({
					"content":jQuery("#bespokeType").val(),
					"intention":"5",
					"createDate":jQuery("#createDate",".table-editor-bespoke").val(),
					"nextDate":jQuery("#promiseDate").val(),
					"userid":customer.userid,
					"username":customer.username,
					"customerId":customer.customerid,
					"status":"0",
					"invalid":"F"
				});
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/save".replace("{dbname}",dbname),
					data:{
						"entityClass":"com.app.miwawa.api.esm.entity.customer.Selling",
						"beans":selling
					},
					success:function(data){
						jQuery("#table-bespoke").bootstrapTable('refresh');
						jQuery("#table-selling").bootstrapTable('refresh');
						return true;		
					}
				});
			}
		}
	}
	
	//删除环信
	function deleteEasemob(customerid){
		jQuery.ajaxsend({
			url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			async:false,
			data:{
	       		"entityClass" : "com.app.miwawa.api.esm.entity.customer.Parents",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customerid
	  	},
	  	success:function(data){
	  		var parents = data.rows;
	  		for(var i=0; i<parents.length; i++){
	  			jQuery.ajaxsend({
	  				url:("/miwawa/{dbname}/parents/delete/{id}".replace("{dbname}", dbname)).replace("{id}",parents[i].id)
	  			});
	  		}
	  	}
		});
	}
	
	//再次跟进
	function followAgain(createDate, promiseDate ,customerId){
		jQuery.ajaxsend({
			url : "/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			data : {
	       		"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customerId,
	          "order[0][sort]": "promiseDate",
	          "order[0][order]":"desc"
	  	},
	  	success : function(data) {
	  		var bespokeList = data.rows;
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
					data:{
				       	"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
				       	"bean":{"id":bespokeList[0].id,"commingState":"2"}
				  },
				  success : function(data) {
				  	jQuery("#table-customer").bootstrapTable("refresh");
				  }
				});
	  	}
		});
	}
	
	//宝宝生日检查
	function checkBirthday(botton, table, form){
		var birthday = $moment(jQuery("#birthday",form).val());
		var nowDate = $moment().format("YYYY-MM-DD");
		if(!$moment(nowDate).isAfter(birthday)){
			jQuery.message.alert("宝宝生日填写有误,请重新填写");
			return;
		}
		handlerSubmit(botton, table, form);
	}
	
//-->
/*]]>*/
</script>
